package com.summer.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.reggie.dto.DishDto;
import com.summer.reggie.entity.Dish;
import com.summer.reggie.entity.DishFlavor;
import com.summer.reggie.mapper.DishMapper;
import com.summer.reggie.service.DishFlavorService;
import com.summer.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 作者:夏天
 * qq:1205232048
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    /**
     * 新增菜品 同时保存对应的口味数据
     * @param dishDto
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //菜品id
        Long dishId = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息 Dish
        Dish dish = this.getById(id);
        //将菜品基本信息拷贝给DishDto
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //条件构造
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //等值条件
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        //通过条件查询到口味集合
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        //将口味赋值给dishDto对象
        dishDto.setFlavors(flavors);
        //将dishDto对象返回
        return dishDto;
    }

    /**
     * 修改菜品信息及口味
     * @param dishDto
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //更新菜品表基本信息
        this.updateById(dishDto);
        //先清除当前菜品对应的口味信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        //再添加前端传过来的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //由于flavors中的属性dishId得不到 所以我们通过该方法去通过dishDto.getId()获得
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //然后将口味信息批量保存到口味表中
        dishFlavorService.saveBatch(flavors);
    }
}
